package com.situ.handler;

import cn.dev33.satoken.exception.DisableServiceException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.situ.ex.*;
import com.situ.exception.ExcelEcxeption;
import com.situ.exception.ManagerException;
import com.situ.utils.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 统一处理异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result handleRuntimeException (RuntimeException e) {
        e.printStackTrace();
        return Result.failureError(500, e.getMessage());
    }

    /**
     * 统一处理excel异常
     * @param e
     * @return
     */
    @ExceptionHandler(ExcelEcxeption.class)
    public Result handleRuntimeException (ExcelEcxeption e) {
        e.printStackTrace();
        return Result.failureError(500, e.getMessage());
    }

    /**
     * 统一处理参数异常
     * @param e
     * @return
     */
    @ExceptionHandler(ParamErrorException.class)
    public Result handleException(ParamErrorException e) {
        e.printStackTrace();
        return Result.failureError(500, e.getMessage() != null ? e.getMessage() :"参数异常");
    }

    /**
     * 统一处理购物车异常
     * @param e
     * @return
     */
    @ExceptionHandler(CartException.class)
    public Result handleException(CartException e) {
        e.printStackTrace();
        return Result.failureError(400, e.getMessage() != null ? e.getMessage() : "购物车异常");
    }

    /**
     * 统一处理用户未登录异常
     * @param e
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public Result handleException(NotLoginException e) {
        e.printStackTrace();
        return Result.failureError(401, "用户未登录");
    }

    /**
     * 统一处理用户非法访问异常
     * @param e
     * @return
     */
    @ExceptionHandler(AttackException.class)
    public Result handleException(AttackException e) {
        e.printStackTrace();
        return Result.failureError(400, e.getMessage() != null ? e.getMessage() : "用户非法访问");
    }

    /**
     * 统一处理用户各种异常
     * @param e
     * @return
     */
    @ExceptionHandler(UserException.class)
    public Result handleException(UserException e) {
        e.printStackTrace();
        return Result.failureError(400, e.getMessage());
    }



    @ExceptionHandler(cn.dev33.satoken.exception.NotLoginException.class)
    public Result handleException(cn.dev33.satoken.exception.NotLoginException e) {
        e.printStackTrace();
        return Result.failureError(401, e.getMessage());
    }
    @ExceptionHandler(NotRoleException.class)
    public Result handleException(NotRoleException e) {
        e.printStackTrace();
        return Result.failureError(403, "角色权限不足,无法访问");
    }
    @ExceptionHandler(NotPermissionException.class)
    public Result handleException(NotPermissionException e) {
        e.printStackTrace();
        return Result.failureError(403, "无此权限");
    }
    @ExceptionHandler(DisableServiceException.class)
    public Result handleException(DisableServiceException e) {
        e.printStackTrace();
        return Result.failureError(403, "账号被封禁");
    }



    /**
     * 统一处理管理员异常
     * @param e
     * @return
     */
    @ExceptionHandler(ManagerException.class)
    public Result handleException(ManagerException e) {
        e.printStackTrace();
        return Result.failureError(500, e.getMessage());
    }


    /**
     * 统一处理业务异常
     * @param e
     * @return
     */
    @ExceptionHandler({ServiceException.class})
    public Result handleException(Throwable e) {
        e.printStackTrace();
        Result result = null;
        if (e instanceof UsernameDuplicatedException) {
            result = Result.failureError(400, "用户名已被注册");
        } else if (e instanceof AddressCountLimitException) {
            result = Result.failureError(4001, "用户的收货地址超出上限");
        } else if (e instanceof AddressNotFoundException) {
            result = Result.failureError(4002, "用户的收货地址数据不存在");
        } else if (e instanceof AccessDeniedException) {
            result = Result.failureError(4003, "收货地址数据非法访问异常");
        } else if (e instanceof UserNotFoundException) {
            result = Result.failureError(4004, "用户名或密码错误");
        } else if (e instanceof ProductNotFoundException) {
            result = Result.failureError(4005, "产品未找到");
        } else if (e instanceof CartNotFoundException) {
            result = Result.failureError(4006, "购物车信息未找到");
        } else if (e instanceof SaveInfoException) {
            result = Result.failureError(5000, "添加信息时出现异常");
        } else if (e instanceof DeleteInfoException) {
            result = Result.failureError(5001, "删除数据时发生未知异常");
        } else if (e instanceof PasswordNotMatchException) {
            result = Result.failureError(5002, "密码错误");
        } else if (e instanceof UpdateException) {
            result = Result.failureError(5003, "更新数据时产生未知异常");
        } else if (e instanceof OrderNotFoundException) {
            result = Result.failureError(5004, "订单信息未找到");
        }
        return result;
    }
}